package com.wh.process.cprocessinstance;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * 流程定义
 *
 * @author WeiHong
 * @date 2018/5/8
 */
public class ProcessInstanceTest {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();


    /**
     * 部署流程定义 by zip
     */
    @Test
    public void deploymentProcessDefinitionZip() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/MyProcess.zip");
        ZipInputStream zis = new ZipInputStream(in);
        Deployment deploy = processEngine.getRepositoryService().createDeployment()
                .name("流程定义C")
                .addZipInputStream(zis)
                .deploy();
        System.out.println("部署ID = " + deploy.getId());
        System.out.println("部署名称 = " + deploy.getName());
    }


    /**
     * 启动流程实例
     */
    @Test
    public void startProcessInstance() {
        String processDefinitionKey = "helloworld";
        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
        System.out.println("流程实例ID = " + processInstance.getId());
        System.out.println("流程定义ID = " + processInstance.getProcessDefinitionId());

    }


    /**
     * 查询个人任务
     */
    @Test
    public void findMyPersonalTask() {
        String taskAssignee = "张三";
        List<Task> list = processEngine.getTaskService().createTaskQuery().taskAssignee(taskAssignee).orderByTaskCreateTime().asc().list();
        for (Task task : list) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务的创建时间:" + task.getCreateTime());
            System.out.println("任务的办理人:" + task.getAssignee());
            System.out.println("流程实例ID：" + task.getProcessInstanceId());
            System.out.println("执行对象ID:" + task.getExecutionId());
            System.out.println("流程定义ID:" + task.getProcessDefinitionId());
            System.out.println("########################################################");
        }
    }

    /**
     * 完成我的任务
     */
    @Test
    public void completeMyPersonalTask() {
        String taskId = "17502";
        processEngine.getTaskService().complete(taskId);
        System.out.println("taskId = " + taskId);
    }

    /**
     * 查询流程状态
     */
    @Test
    public void isProcessEnd() {
        String processInstanceId = "";
        ProcessInstance pi = processEngine.getRuntimeService().createProcessInstanceQuery()
                .processInstanceId(processInstanceId).singleResult();
        if (null == pi) {
            System.out.println("流程结束！");
        } else {
            System.out.println("流程未结束！");
        }
    }

    /**
     * 查询历史任务
     */
    @Test
    public void findHistoryTask() {
        String taskAssingnee = "张三";
        List<HistoricTaskInstance> list = processEngine.getHistoryService()
                .createHistoricTaskInstanceQuery()
                .taskAssignee(taskAssingnee)
                .list();
        for (HistoricTaskInstance hti : list) {
            System.out.println(hti.getId() + "    " + hti.getName() + "    " + hti.getProcessInstanceId() + "   " + hti.getStartTime() + "   " + hti.getEndTime() + "   " + hti.getDurationInMillis());
            System.out.println("################################");
        }
    }

    /**
     * 查询历史流程实例
     */
    @Test
    public void findHistoryProcessInstance(){
        String processInstanceId = "1001";
        HistoricProcessInstance hpi = processEngine.getHistoryService()
                .createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        System.out.println(hpi.getId()+"    "+hpi.getProcessDefinitionId()+"    "+hpi.getStartTime()+"    "+hpi.getEndTime()+"     "+hpi.getDurationInMillis());
    }

}
